package com.yunatt.service.impl;

import com.yunatt.mapper.AccessWeekMapper;
import com.yunatt.mapper.DeviceMapper;
import com.yunatt.mapper.MachineCommandMapper;
import com.yunatt.model.AccessWeekEntity;
import com.yunatt.model.DeviceEntity;
import com.yunatt.model.MachineCommandEntity;
import com.yunatt.service.AccessWeekService;
import com.yunatt.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AccessWeekServiceImpl implements AccessWeekService {
    @Autowired
    private AccessWeekMapper accessWeekMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private MachineCommandMapper machineCommandMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return accessWeekMapper.deleteById(id);
    }

    @Override
    public int insert(AccessWeekEntity record) {
        return accessWeekMapper.insert(record);
    }

    @Override
    public int insertSelective(AccessWeekEntity record) {
        return accessWeekMapper.insert(record);
    }

    @Override
    public AccessWeekEntity selectByPrimaryKey(Integer id) {
        return accessWeekMapper.selectById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(AccessWeekEntity record) {
        return accessWeekMapper.updateById(record);
    }

    @Override
    public int updateByPrimaryKey(AccessWeekEntity record) {
        return accessWeekMapper.updateById(record);
    }

    @Override
    public List<AccessWeekEntity> selectAllAccessWeek() {
        return accessWeekMapper.selectAllAccessWeek();
    }

    @Override
    public void setAccessWeek() {
        StringBuilder sb=new StringBuilder();
        sb.append("{\"cmd\":\"setdevlock\",\"weekzone\":[");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<AccessWeekEntity>accessWeeks=accessWeekMapper.selectAllAccessWeek();
        List<AccessWeekEntity>accessWeeksTemp=new ArrayList<AccessWeekEntity>();
        if(accessWeeks.size()==8){
            for (int i = 0; i < accessWeeks.size(); i++) {
                sb.append("{\"week\":[");
                sb.append("{\"day\":" + accessWeeks.get(i).getSunday() + "},");
                sb.append("{\"day\":" + accessWeeks.get(i).getMonday() + "},");
                sb.append("{\"day\":" + accessWeeks.get(i).getTuesday() + "},");
                sb.append("{\"day\":" + accessWeeks.get(i).getWednesday() + "},");
                sb.append("{\"day\":" + accessWeeks.get(i).getThursday() + "},");
                sb.append("{\"day\":" + accessWeeks.get(i).getFriday() + "},");
                sb.append("{\"day\":" + accessWeeks.get(i).getSaturday() + "}");
                if (i != 7) {
                    sb.append("]},");
                } else {
                    sb.append("]}");
                }
            }
        }else if(accessWeeks.size()<8){
            accessWeeksTemp.addAll(accessWeeks);
            for (int i = accessWeeks.size(); i < 8; i++) {
                AccessWeekEntity accessWeek=new AccessWeekEntity();
                accessWeek.setId(i+1);
                accessWeek.setMonday(0);
                accessWeek.setTuesday(0);
                accessWeek.setWednesday(0);
                accessWeek.setThursday(0);
                accessWeek.setFriday(0);
                accessWeek.setSaturday(0);
                accessWeek.setSunday(0);
                accessWeeksTemp.add(i, accessWeek);

            }

            for (int i = 0; i < accessWeeksTemp.size(); i++) {
                sb.append("{\"week\":[");
                sb.append("{\"day\":" + accessWeeksTemp.get(i).getSunday() + "},");
                sb.append("{\"day\":" + accessWeeksTemp.get(i).getMonday() + "},");
                sb.append("{\"day\":" + accessWeeksTemp.get(i).getTuesday() + "},");
                sb.append("{\"day\":" + accessWeeksTemp.get(i).getWednesday() + "},");
                sb.append("{\"day\":" + accessWeeksTemp.get(i).getThursday() + "},");
                sb.append("{\"day\":" + accessWeeksTemp.get(i).getFriday() + "},");
                sb.append("{\"day\":" + accessWeeksTemp.get(i).getSaturday() + "}");
                if (i != 7) {
                    sb.append("]},");
                } else {
                    sb.append("]}");
                }
            }
        }

        sb.append("]}");
        String message=sb.toString();
        List<DeviceEntity>deviceList=deviceMapper.findAllDevice();
        for (int i = 0; i < deviceList.size(); i++) {

            MachineCommandEntity machineCommand=new MachineCommandEntity();
            machineCommand.setContent(message);
            machineCommand.setName("setdevlock");
            machineCommand.setStatus(0);
            machineCommand.setSendStatus(0);
            machineCommand.setErrCount(0);
            machineCommand.setSerial(deviceList.get(i).getSerialNum());
            machineCommand.setGmtCrate(new Date());
            machineCommand.setGmtModified(new Date());

            //	machineComandService.addMachineCommand(machineCommand);
            machineCommandMapper.insert(machineCommand);
        }
    }
}
